package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TengXun1 {
    public static void main(String[] args) {
        String ip = "192.168.1.1/8";
        String[] ips = new String[5];
        System.out.println(getRes(ips));

    }

    private static Map<String, List<String>> getRes(String[] ips) {
        Map<String, List<String>> res = new HashMap<>();
        if (ips == null || ips.length < 1){
            return res;
        }
        for (int i = 0; i < ips.length; i++) {
            String[] ipNum = ips[i].split("/");
            String ip = ipNum[0];
            int num = Integer.parseInt(ipNum[1]);
            String wangLuoNum = getWangLuoNumW(ip,num);
            if (res.containsKey(wangLuoNum)){
                res.get(wangLuoNum).add(ips[i]);
            }else {
                List<String> temp = new ArrayList<>();
                temp.add(ips[i]);
                res.put(wangLuoNum,temp);
            }
        }
        return res;
    }

    private static String getWangLuoNumW(String ip, int num) {
        String[] ips = ip.split(".");
        StringBuilder stringBuilder = new StringBuilder();
        if (num == 8){
            for (int i = 0; i < 3; i++) {
                stringBuilder.append(ips[i]);
                stringBuilder.append(".");
            }
            stringBuilder.append(0);
        }else if (num == 16){
            for (int i = 0; i < 2; i++) {
                stringBuilder.append(ips[i]);
                stringBuilder.append(".");
            }
            stringBuilder.append(0 + "." + 0);
        }else {

        }
        return stringBuilder.toString();
    }

    public int compare(String str1,String str2){
        String[] strs1 = str1.split(".");
        String[] strs2 = str2.split(".");
        if (str1.length() != str2.length()){
            return 0;
        }
        for (int i = 0; i < strs1.length; i++) {
            if (Integer.parseInt(strs1[i]) > Integer.parseInt(strs2[i])){
                return 1;
            }else if (Integer.parseInt(strs1[i]) < Integer.parseInt(strs2[i])){
                return -1;
            }
        }
        return 0;
    }
}
